package com.console.operations.planmgr;

import java.math.BigDecimal;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.console.pages.planmgr.FinancePlanDepositPage;
import com.console.util.FinancePlanDepositUtil;

	/**
		@author 王嘉犇
		@name 钜宝盆后台理财计划前台发布或代投
		@type 动作类
	 */

public class JbpFinancePlanFrontReleaseOrDeposit extends OperationBase {

	public JbpFinancePlanFrontReleaseOrDeposit(UITestBase tb){
		super(tb);
	}
	
	public boolean isstatuscorrectedafterfrontrelease = false;
	
	//前台发布
	public void frontrelease(String financeplantitle) throws Exception{
		switchToDefaultContent();
		FinancePlanDepositPage fpdp = new FinancePlanDepositPage(tb);
		//防止页面刷新导致点击报错的处理
		elementToBeClickable(fpdp.panelheaddiv);
		//到新页面后，直到投资链接可视
		waitForElementDisplayed(fpdp.panelheaddiv);
		//动态设置DefaultElement的findValue
		fpdp.frontreleaseLnk.setfindValue("//tr[td[text()='"+ financeplantitle +"']]/td[9]/a[2]");
		elementToBeClickable(fpdp.pagination);
		waitForElementDisplayed(fpdp.pagination);
		//得到总页数控件下tag为li的子控件集合
		List<WebElement> subelement = fpdp.pagination.getNativeElement().findElements(By.tagName("li"));
		int pagecount = 0;
		//得到分页的页数
		if(subelement.size()>1){
			pagecount = Integer.parseInt(subelement.get(subelement.size()-1).getText());
		}else if(subelement.size()==1){
			pagecount = 1;
		}
		log.info("page count is " + pagecount);
		if(pagecount>1){
			for(int i=1;i<pagecount;i++){
				if(isPageContainsStr(financeplantitle)){
					waitForElementExist(fpdp.frontreleaseLnk);
					fpdp.frontreleaseLnk.click();
					break;
				}else{
					fpdp.pageLink.setfindValue("//li[a[text()='"+ (i+1) +"']]/a");
					fpdp.pageLink.click();
					pause(1000);
				}
			}
		}else{
			waitForElementDisplayed(fpdp.frontreleaseLnk);
			fpdp.frontreleaseLnk.click();
		}
		pause(1000);
		String dcstatus = DBUtil.queryOneCell("select dc_status from fiz_plan where vc_name = ?", new Object[]{financeplantitle});
		//点击前台发布后，判断理财计划是否为前台募集中
		isstatuscorrectedafterfrontrelease = dcstatus.equals("01");
		if(!isstatuscorrectedafterfrontrelease){
			log.error("点击前台发布后状态没有变为01（前台筹集中）");
			return;
		}
	}
	
	public void deposit(String financeplantitle) throws Exception{
		switchToDefaultContent();
		FinancePlanDepositPage fpdp = new FinancePlanDepositPage(tb);
		//防止页面刷新导致点击报错的处理
		elementToBeClickable(fpdp.panelheaddiv);
		//到新页面后，直到投资链接可视
		waitForElementDisplayed(fpdp.panelheaddiv);
		//动态设置DefaultElement的findValue，取得对应理财计划这行的投资链接
		fpdp.depositLink.setfindValue("//tr[td[text()='"+ financeplantitle +"']]/td[9]/a[1]");
		elementToBeClickable(fpdp.pagination);
		waitForElementDisplayed(fpdp.pagination);
		/*
		 * 下边是理财计划代投初始页面有多页，且理财计划名称不在首页的情况下
		 * 的处理方式
		 */
		//得到分页的页数
		int pagecount = Integer.parseInt(fpdp.lastpageLink.getText());
		log.info("分页页数为  " + pagecount);
		if(pagecount>1){
			for(int i=1;i<pagecount;i++){
				if(isPageContainsStr(financeplantitle)){
					waitForElementExist(fpdp.depositLink);
					fpdp.depositLink.click();
					break;
				}else{
					fpdp.pageLink.setfindValue("//li[a[text()='"+ (i+1) +"']]/a");
					fpdp.pageLink.click();
					pause(1000);
				}
			}
		}else{
			waitForElementDisplayed(fpdp.depositLink);
			fpdp.depositLink.click();
		}
		String nbsurplusbeforedepositestr = DBUtil.queryOneCell("select nb_surplus\n" +
												  				"from fiz_plan\n" +
												  				"where vc_name = ?", new Object[]{financeplantitle});
		BigDecimal nbsurplusbeforedepositebd = new BigDecimal(nbsurplusbeforedepositestr).setScale(2, BigDecimal.ROUND_DOWN);
		log.info("理财计划剩余可投资金额为 " + nbsurplusbeforedepositebd);
		BigDecimal leftusercashaccountbd = new BigDecimal(DBUtil.queryOneCell("select nb_balance\n"
																			+ "from fiz_user_account\n"
																			+ "where fk_user_id = (\n"
																			+ "                    select pk_id\n"
																			+ "                    from fiz_user\n"
																			+ "                    where vc_cellphone = ?\n"
																			+ "                   )"
																			+ "  and dc_type = ?", new Object[]{FinancePlanDepositUtil.MOBILE, "00"})).setScale(2, BigDecimal.ROUND_DOWN);
		log.info("投资用户现金账户余额为 " + leftusercashaccountbd);
		pause(1000);
		fpdp.cellphoneinput.type(FinancePlanDepositUtil.MOBILE);
		pause(700);
		fpdp.searchButton.click();
		pause(700);
		if(leftusercashaccountbd.compareTo(new BigDecimal(FinancePlanDepositUtil.DEPOSITFEE).setScale(2, BigDecimal.ROUND_DOWN))==-1){
			log.info("投资用户现金账户余额不足，投资金额输入框将会失效");
			waitForElementDisabled(fpdp.amountinput);
		}else{
			pause(700);
			fpdp.searchButton.click();
			pause(700);
			fpdp.amountinput.type(FinancePlanDepositUtil.DEPOSITFEE);
			if(nbsurplusbeforedepositebd.compareTo(new BigDecimal(FinancePlanDepositUtil.DEPOSITFEE).setScale(2, BigDecimal.ROUND_DOWN))==-1){
				log.info("投标金额大于理财计划剩余可购买金额，确定投资按钮将会失效");
				waitForElementDisabled(fpdp.assuredepositButton);
			}else{
				fpdp.assuredepositButton.click();
				waitForPageContainsStr("投资成功");
			}
		}
	}
}
